package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.impl;

import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.ProLosInfo;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.ProLosInfoRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class ProLosInfoRepositoryImpl implements ProLosInfoRepository {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Override
    public void deleteByXlrmId(String xlrmId) {
        String sql = "delete from  xczh_property_loss_info  where xlrm_id= '"+xlrmId+"'" ;
        jdbcTemplate.update(sql);
    }

    @Override
    public void save(ProLosInfo proLosInfo) {
        String sql = "insert into xczh_property_loss_info(xpli_id,province,city,county,township,village,project_category,metering_unit," +
                "unit_price,quantity,loss_value,remark,tel,fill_form_user,fill_form_time,xlrm_id,quantity_total,loss_value_total,indoor_and_outdoor) " +
                "values(:xpliId, :province, :city, :county, :township, :village, :projectCategory, :meteringUnit, :unitPrice, " +
                ":quantity, :lossValue, :remark, :tel, :fillFormUser, :fillFormTime, :xlrmId, :quantityTotal, :lossValueTotal, :indoorAndOutdoor)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(proLosInfo);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<ProLosInfo> getProLosInfoByXsemId(String xsemId, String reportType) {
        String sql = "select t2.xsem_id,t1.province,t1.city,t1.county,t1.indoor_and_outdoor,(CAST(t1.loss_value AS DECIMAL(15,2))) as lossValue from xczh_property_loss_info t1 left join xczh_loss_report_manage t2 on t1.xlrm_id=t2.id" +
                " where t2.xsem_id = '"+xsemId+"' and t2.report_type = '"+reportType+"' and loss_value is not null and loss_value!=''";
        List<ProLosInfo> list = namedParameterJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ProLosInfo.class));
        return list;
    }
}
